using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using UnityEngine;
using UnityEngine.Assertions;

public class StockManager : MonoBehaviour
{
    //股票代码列表
    private Dictionary<int, string> codeMap = new();
    //保持所有股票的数据
    private Dictionary<int, Dictionary<string, StockDayData>> allStockData = new();
    

    //加载基本的股票信息
    public void Awake()
    {
        codeMap.Clear();
        string[] lineArray =
            Utility.SafeReadAllLine("python/stk_data/stk_list.csv", Utility.UnityPathType.persistentDataPath);

        for (int i = 1; i < lineArray.Length; i++)
        {
            string line = lineArray[i].Replace(".", "");

            string kcCode = line.Substring(2, 3);
            //排除科创板
            if (kcCode == "688")
            {
                continue;
            }

            codeMap.Add(line.GetHashCode(), line);
        }

        Debug.LogError(DateTime.Today);

        string d1 = DateTime.Today.ToString("yy-MM-dd");
        Debug.LogError(d1);
    }

    //获取指定股票指定日期的信息
    public StockDayData GetStockPeriodData(int code, string day)
    {
        DataSet dataset = AutoTrade.sqlAccess.ExecuteSQL($"select * from {code} where date between '{day}' and '{day}'");
        Assert.IsNotNull(dataset);
        DataTable dataTable = dataset.Tables[0];
        int rows = dataTable.Rows.Count;
        if (rows != 1)
        {
            Debug.LogError("rows != 1");
            return null;
        }
        
        StockDayData stockPeriodData = new StockDayData();
        for (int i = 0; i < rows; i++)
        {
            string date = dataTable.Rows[i]["date"].ToString();
            float open = float.Parse(dataTable.Rows[i]["open"].ToString());
            float high = float.Parse(dataTable.Rows[i]["high"].ToString());
            float low = float.Parse(dataTable.Rows[i]["low"].ToString());
            float close = float.Parse(dataTable.Rows[i]["close"].ToString());
            long volume = long.Parse(dataTable.Rows[i]["volume"].ToString());
            long amount = long.Parse(dataTable.Rows[i]["amount"].ToString());
            float turn = float.Parse(dataTable.Rows[i]["turn"].ToString());
            float pctChg = float.Parse(dataTable.Rows[i]["pctChg"].ToString());


            StockDayData dayData = new StockDayData();
            dayData.date = date;
            dayData.code = code.GetHashCode();

            // if(i>0)
            //     dayData.lastClose = stockPeriodData.dayDatas[i - 1].close;

            dayData.open = open;
            dayData.high = high;
            dayData.low = low;
            dayData.close = close;
            dayData.volume = volume;
            dayData.amount = amount;
            dayData.turn = turn;
            dayData.pctChg = pctChg;
        }
        
        //添加股票每日数据
        AddStockDayData(code, stockPeriodData);
        return stockPeriodData;
    }
    
    //获取股票的指定日期的数据
    public StockDayData GetStockDayData(int code, string day)
    {
        //缓存有没有
        if (allStockData.ContainsKey(code))
        {
            if (allStockData[code].ContainsKey(day))
            {
                return allStockData[code][day];
            }
        }
        return GetStockPeriodData(code, day);
    }
    
    
    //添加股票每日数据
    private void AddStockDayData(int code, StockDayData data)
    {
        if (!allStockData.ContainsKey(code))
        {
            allStockData.Add(code, new Dictionary<string, StockDayData>());
        }
        
        allStockData[code].Add(data.date, data);
    }
}
